package com.aries.ui.widget.demo.adapter;

/**
 * Created: AriesHoo on 2017/7/14 9:55
 * Function: TitleBarView 适配器
 * Desc:
 */

import com.aries.ui.widget.demo.ResourceTable;
import com.aries.ui.widget.demo.entity.TitleEntity;
import com.aries.ui.widget.demo.util.LogUtil;
import ohos.agp.components.*;

import java.util.List;

/**
 * @Author: AriesHoo on 2019/7/17 15:02
 * @E-Mail: AriesHoo@126.com
 * @Function:
 * @Description:
 */
public class TitleAdapter extends BaseItemProvider {

    private DirectionalLayout mHeaderLayout;

    private List<TitleEntity> datas;
    private boolean mHeadAndEmptyEnable = false;

    public TitleAdapter(List<TitleEntity> datas) {
        this.datas = datas;
    }

    @Override
    public int getCount() {
        if (mHeaderLayout != null) {
            return datas.size() + 1;
        }
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        if (mHeaderLayout != null) {
            if(i == 0){
                return null;
            }else{
                return datas.get(i - 1);
            }
        }
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        if (mHeaderLayout != null && position == 0) {
            return mHeaderLayout;
        }
        Component layout;
        if (component == null) {
            component = LayoutScatter.getInstance(componentContainer.getContext())
                    .parse(ResourceTable.Layout_item_widget, null, false);
            component.setTag(new BaseViewHolder(component));
        }
        layout = component;
        if(mHeaderLayout != null){
            convert(position, (BaseViewHolder) component.getTag(), datas.get(position - 1));
        }else{
            convert(position, (BaseViewHolder) component.getTag(), datas.get(position));
        }
        return layout;
    }

    protected void convert(int position, BaseViewHolder helper, TitleEntity item) {
        helper.setText(ResourceTable.Id_tv_titleWidget, item.title);
        helper.setText(ResourceTable.Id_tv_contentWidget, item.content);
//                position == datas.size() - 1 ? NavigationBarUtil.getNavigationBarHeight(helper.itemView.getContext()) : 0);
    }

    public void addHeaderView(Component header, int index, int orientation) {
        mHeadAndEmptyEnable = true;
        if (mHeaderLayout == null) {
            mHeaderLayout = new DirectionalLayout(header.getContext());
            int MATCH_PARENT = DirectionalLayout.LayoutConfig.MATCH_PARENT;
            int MATCH_CONTENT = DirectionalLayout.LayoutConfig.MATCH_CONTENT;
            if (orientation == DirectionalLayout.VERTICAL) {
                mHeaderLayout.setOrientation(DirectionalLayout.VERTICAL);
                mHeaderLayout.setLayoutConfig(new DirectionalLayout.LayoutConfig(MATCH_PARENT, MATCH_CONTENT));
            } else {
                mHeaderLayout.setOrientation(DirectionalLayout.HORIZONTAL);
                mHeaderLayout.setLayoutConfig(new DirectionalLayout.LayoutConfig(MATCH_CONTENT, MATCH_PARENT));
            }
        }
        final int childCount = mHeaderLayout.getChildCount();
        if (index < 0 || index > childCount) {
            index = childCount;
        }
        mHeaderLayout.addComponent(header, index);
        if (mHeaderLayout.getChildCount() == 1) {
            int position = getHeaderViewPosition();
            if (position != -1) {
                notifyDataSetItemInserted(position);
            }
        }
    }

    private int getHeaderViewPosition() {
        if (getEmptyViewCount() == 1) {
            if (mHeadAndEmptyEnable) {
                return 0;
            }
        } else {
            return 0;
        }
        return -1;
    }

    public int getEmptyViewCount() {
        if (datas.size() != 0) {
            return 0;
        }
        return 1;
    }

    public static class BaseViewHolder {

        public Component itemView;

        public BaseViewHolder(Component itemView) {
            this.itemView = itemView;
        }

        public void setText(int resId, String text) {
            Text tv = (Text) itemView.findComponentById(resId);
            tv.setText(text);
        }

    }

}
